package git.soulbgm.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * RestTemplate配置
 *
 * @author SoulBGM
 * @date 2022/01/14
 */
@Configuration
public class RestTemplateConfig {

    /**
     * 连接超时
     */
    private int connectTimeout = 1000;
    /**
     * 读取超时
     */
    private int readTimeout = 2000;

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory){
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        //客户端与服务端建立连接超时时间
        factory.setConnectTimeout(connectTimeout);
        //客户端从服务端读取数据的超时时间
        factory.setReadTimeout(readTimeout);
        return factory;
    }

}
